package com.homedo.microservice.open.service.gateway.alert.gateway.persistence.dao;

import com.homedo.common.bean.vo.Pagination;
import com.homedo.common.dao.BaseDao;
import com.homedo.common.util.Detect;
import com.homedo.microservice.open.service.gateway.alert.gateway.bean.enums.forroute.StatusEnum4Route;
import com.homedo.microservice.open.service.gateway.alert.gateway.bean.po.Route;
import com.homedo.microservice.open.service.gateway.alert.gateway.bean.query.RouteQuery;
import com.homedo.microservice.open.service.gateway.alert.gateway.persistence.mapper.IRouteMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.stream.Collectors;


/**
 * @author 作者 jgshun
 * @date 创建时间 2018年09月18日 11时30分20秒
 */
@Repository
public class RouteDao extends BaseDao<Route> {
    @Autowired
    private IRouteMapper routeMapper;

    public List<Route> getByApplication(String applicationName, List<StatusEnum4Route> statuses) {
        Example example = new Example(Route.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo(Route.FIELD_APPLICATIONNAME, applicationName);
        if (Detect.notEmpty(statuses)) {
            criteria.andIn(Route.FIELD_STATUS, statuses.stream().map(StatusEnum4Route::getCode).collect(Collectors.toList()));
        }

        example.setOrderByClause(Route.ORDER_SORT_ASC);

        return super.getListByExample(example);
    }

    public Pagination<Route> getPagination(RouteQuery routeQuery, Pagination<Route> pagination) {
        Example example = new Example(Route.class);
        Example.Criteria criteria = example.createCriteria();

        if (Detect.notEmpty(routeQuery.getApplicationNames())) {
            criteria.andIn(Route.FIELD_APPLICATIONNAME, routeQuery.getApplicationNames());
        }
        if (Detect.notEmpty(routeQuery.getLikeComment())) {
            criteria.andLike(Route.FIELD_COMMENT, "%" + routeQuery.getLikeComment() + "%");
        }
        if (Detect.notEmpty(routeQuery.getLikeId())) {
            criteria.andLike(Route.FIELD_ID, "%" + routeQuery.getLikeId() + "%");
        }
        if (Detect.notEmpty(routeQuery.getLikePath())) {
            criteria.andLike(Route.FIELD_PATH, "%" + routeQuery.getLikePath() + "%");
        }

        if (Detect.notEmpty(routeQuery.getLikeServiceId())) {
            criteria.andLike(Route.FIELD_SERVICEID, "%" + routeQuery.getLikeServiceId() + "%");
        }

        if (Detect.notEmpty(routeQuery.getDomainRouteIds())) {
            criteria.andIn(Route.FIELD_DOMAINROUTEID, routeQuery.getDomainRouteIds());
        }
        if (Detect.notEmpty(routeQuery.getNotInServiceIds())) {
            criteria.andNotIn(Route.FIELD_SERVICEID, routeQuery.getNotInServiceIds());
        }
        if (Detect.notEmpty(routeQuery.getStatuses())) {
            criteria.andIn(Route.FIELD_STATUS, routeQuery.getStatuses());
        }

        return super.getPagination(pagination, example);
    }
}
